<template>
  <div class="login-container">
    <div class="loginCard">
      <div class="login-sidebox">
        <div class="login-sidebox__title">
          <h2>HuoCMS</h2>
          <p>高性能 / 精致 / 优雅</p>
          <p>多语言,多站点,强大的SEO功能</p>
        </div>
        <img src="@/assets/loginbg.png">
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        autocomplete="on"
        label-position="left"
      >
        <div class="title-container">
          <img src="@/assets/logo.png" alt="">
          <h2 class="title">欢迎您登录！</h2>
        </div>
        <el-form-item prop="account">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="account"
            v-model="loginForm.account"
            placeholder="用户名"
            name="account"
            type="text"
            tabindex="1"
            autocomplete="on"
          />
        </el-form-item>
        <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="密码"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
        </el-tooltip>
        <el-button
          :loading="loading"
          class="loginBtn"
          @click.native.prevent="handleLogin"
        >登录</el-button>
      </el-form>

    </div>

    <!-- <el-dialog title="Or connect with" :visible.sync="showDialog">
      Can not be simulated on local, so please combine you own business simulation! ! !
      <br>
      <br>
      <br>
      <social-sign />
    </el-dialog> -->
  </div>
</template>

<script>
// import { validaccount } from '@/utils/validate'
// import SocialSign from './components/SocialSignin'
import { getInfo } from '@/api/user'

export default {
  name: 'Login',
  // components: { SocialSign },
  data() {
    return {
      loginForm: {
        account: 'admin@admin.com',
        password: '123123'
      },
      loginRules: {
        account: [
          { required: 'true', message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: 'true', message: '请输入密码', trigger: 'blur' }
        ]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },

  mounted() {
    if (this.loginForm.account === '') {
      this.$refs.account.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },

  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    getUserInfo() {
      getInfo().then(res => {
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then((res) => {
              if (res.data.token) {
                this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
                this.loading = false
                this.getUserInfo()
                this.$message.success(res.msg)
              } else {
                this.$message.error(res.msg)
                this.loading = false
              }
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
    // afterQRScan() {
    //   if (e.key === 'x-admin-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }
  }
}
</script>

<style lang="scss">
#app .el-button.loginBtn{
  margin-top: 30px;
  border-radius: 0;
  border-color: #f83600;
}
#app .el-button.loginBtn:active{
  border-color: #f83600;
}
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: #000;
  }
}

/* reset element-ui css */
 .login-container .el-form-item{
  border: none !important;
  border-bottom:  solid 1px #ddd !important;
}
.login-container {
  height: 100vh;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
// input:-internal-autofill-selected{
//   background-color: #fff !important;
// }
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;

      &:-webkit-autofill {
        // 自动填充input样式
        // box-shadow: 0 0 0px 1000px #000 inset !important;
        -webkit-text-fill-color: #000 !important;
        background-color: #fff;
      }
    }
  }

  .el-form-item {
    width: 80%;
    margin: 0 auto;
    margin-bottom: 20px;
    border: 1px solid #ddd;
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .el-form-item__content{
  line-height: 33px;
}
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #fff;
  overflow: hidden;
  position: relative;

.loginCard{
  width: 85%;
  max-width: 1030px;
  height: 670px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  justify-content: space-between;
  box-shadow: 0px 0px 60px 0px
		rgba(0, 0, 0, 0.08);
}

.login-sidebox{
  width: 50%;
  position: relative;
  overflow: hidden;
  .login-sidebox__title{
    margin:220px 0 20px 61px;
    color: #fff;
    h2{
      font-size: 52px;
      letter-spacing: 3px;
      margin-bottom: 6px;
      font-family: Arial ;
    }
    h4{
      line-height: 50px;
      font-weight: normal;
    }
    p{
      font-size: 17px;
      font-weight: normal;
      font-family: MicrosoftYaHeiLight;
      line-height: 24px;
      letter-spacing: 1px;
    }
  }
  img{
    position: absolute;
    top: 0;
    width: 520px;
    height: 670px;
    z-index: -1;
  }
}
  .login-form {
    width: 50%;
    max-width: 100%;
    padding: 105px  0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #000;
    // margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    img{
      margin: 0 auto;
      margin-bottom: 20px;
      display: block;
    }
    .title {
      font-size: 26px;
      color: #333;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #000;
    cursor: pointer;
    user-select: none;
  }
.loginBtn{
  width: 80%;
  margin: 0 auto;
  display: block;
  background-color: #f83600;
  color: #fff;
  border-radius: 0;
}
  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
  @media screen and (max-width:1000px){
    .login-form{
      width: 100%;

    }
    .login-sidebox{
      display: none;
    }
  }
}
</style>
